package top.leix.springdemo.services.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.leix.springdemo.NowSemester;
import top.leix.springdemo.Res;
import top.leix.springdemo.mapper.*;
import top.leix.springdemo.pojo.*;
import top.leix.springdemo.services.AdministratorService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 16435
* @description 针对表【administrator(管理员表)】的数据库操作Service实现
* @createDate 2024-04-21 22:13:35
*/
@Service
public class AdministratorServiceImpl extends ServiceImpl<AdministratorMapper, Administrator>
    implements AdministratorService {
    @Resource
    AdministratorMapper administratorMapper;
    @Resource
    TeacherMapper teacherMapper;
    @Resource
    StudentMapper studentMapper;
    @Resource
    LabAssistantMapper labAssistantMapper;
    @Resource
    TermsMapper termsMapper;
    @Resource
    ClasstableMapper classtableMapper;
    @Override
    public Res addAdministrator(Administrator administrator) {
        int insertCount = administratorMapper.insert(administrator);
        if (insertCount > 0) {
            return Res.ok(administrator);
        } else {
            return Res.failed("添加管理员");
        }
    }

    /**
     * 根据名字查询老师
     */
    @Override
    public Res queryTeacherByName(String userName) {
        List<Teacher> users = teacherMapper.queryTeacherByName(userName);
        return Res.ok(users);
    }

    /**
     * 根据名字查询学生
     */
    @Override
    public Res queryStudentByName(String userName) {
        List<Student> users = studentMapper.queryStudentByName(userName);
        return Res.ok(users);
    }
    /**
     * 根据名字查询实验员
     */
    @Override
    public Res queryLabAssistantByName(String userName) {
        List<LabAssistant> users = labAssistantMapper.queryLabAssistantByName(userName);
        return Res.ok(users);
    }

    /**
     * 查询所有老师
     * @return
     */
    @Override
    public Res queryAllTeacher() {
        List<Teacher> teacherList = teacherMapper.queryAllTeacher();
        return Res.ok(teacherList);
    }

    /**
     * 添加老师
     * @param teacher
     * @return
     */
    @Override
    public Res addTeacher(Teacher teacher) {
        int rows = teacherMapper.addTeacher(teacher);
        if(rows >= 1 ){
            System.out.println("成功添加");
            return Res.ok(null);
        }
        System.out.println("添加失败");
        return Res.failed(null);
    }

    /**
     * 更新老师
     * @param teacher
     * @return
     */
    @Override
    public Res updateTeacher(Teacher teacher) {
        int rows = teacherMapper.updateTeacher(teacher);
        if(rows >= 1 ){
            System.out.println("成功修改");
            return Res.ok(null);
        }
        System.out.println("修改失败");
        return Res.failed(null);
    }

    /**
     * 删除老师by userAccount
     * @param userAccount 账户
     * @return
     */
    @Override
    public Res deleteTeacher(String userAccount) {
        int rows = teacherMapper.deleteUserAccount(userAccount);
        if(rows >= 1 ){
            System.out.println("成功删除");
            return Res.ok(null);
        }
        return Res.failed(null);
    }

    @Override
    public Res queryAllStudent() {
        List<Student> studentList = studentMapper.queryAllStudent();
        return Res.ok(studentList);
    }

    @Override
    public Res addStudent(Student student) {
        int rows = studentMapper.addStudent(student);
        if(rows >= 1 ){
            System.out.println("成功添加");
            return Res.ok(null);
        }
        System.out.println("添加失败");
        return Res.failed(null);
    }

    @Override
    public Res updateStudent(Student student) {
        int rows = studentMapper.updateStudent(student);
        if(rows >= 1 ){
            System.out.println("成功修改");
            return Res.ok(null);
        }
        System.out.println("修改失败");
        return Res.failed(null);
    }

    @Override
    public Res deleteStudent(String userAccount) {
        int rows = studentMapper.deleteUserAccount(userAccount);
        if(rows >= 1 ){
            System.out.println("成功删除");
            return Res.ok(null);
        }
        return Res.failed(null);
    }

    @Override
    public Res queryAllLabAssistant() {
        List<LabAssistant> labAssistantList = labAssistantMapper.queryAllLabAssistant();
        return Res.ok(labAssistantList);
    }

    @Override
    public Res addLabAssistant(LabAssistant labAssistant) {
        int rows = labAssistantMapper.addLabAssistant(labAssistant);
        if(rows >= 1 ){
            System.out.println("成功添加");
            return Res.ok(null);
        }
        System.out.println("添加失败");
        return Res.failed(null);
    }

    @Override
    public Res updateLabAssistant(LabAssistant labAssistant) {
        int rows = labAssistantMapper.updateLabAssistant (labAssistant);
        if(rows >= 1 ){
            System.out.println("成功修改");
            return Res.ok(null);
        }
        System.out.println("修改失败");
        return Res.failed(null);
    }

    @Override
    public Res deleteLabAssistant(String userAccount) {
        int rows = labAssistantMapper.deleteUserAccount(userAccount);
        if(rows >= 1 ){
            System.out.println("成功删除");
            return Res.ok(null);
        }
        return Res.failed(null);
    }

    //Term
    @Override
    public Res queryNowTerm() {
        // 查询当前学期
        Terms nowTerm = termsMapper.selectOne(new QueryWrapper<Terms>().eq("isNow", true));
        return Res.ok(nowTerm);
    }

    @Override
    public Res addTerm(Terms terms) {
        String semester = terms.getSemester();
        QueryWrapper<Terms> queryWrapper = Wrappers.<Terms>query()
                .eq("semester", semester);
        Terms existingTerms = termsMapper.selectOne(queryWrapper);
        if (existingTerms != null) {
            // 如果已存在，返回失败或提示信息
            return Res.failed("学期已存在");
        } else {
            int insertCount = termsMapper.insert(terms);
            if (insertCount > 0) {
                return Res.ok(null);
            } else {
                return Res.failed("添加学期失败");
            }
        }
    }

    @Override
    public Res queryAllTerm() {
        List<Terms> termsList = termsMapper.selectList(new QueryWrapper<Terms>());
        return Res.ok(termsList);
    }

    @Override
    public Res updateTerm(Terms terms) {
        int rows = termsMapper.updateTerm(terms);
        if(rows >= 1 ){
            System.out.println("成功修改学期");
            return Res.ok(null);
        }
        System.out.println("修改学期失败");
        return Res.failed(null);
    }

    @Override
    public Res setNowTerm(Terms terms) {
        if (terms == null || terms.getSemester() == null) {
            return Res.failed("传入的学期对象或学期信息为空");
        }
        // 先将当其他的isNow设置为false
        termsMapper.update(
                null,
                new UpdateWrapper<Terms>().set("isNow", false));

        // 确保只有一个学期的isNow设置为true
        int updateCount = termsMapper.update(
                terms,
                new UpdateWrapper<Terms>().eq("semester", terms.getSemester()).set("isNow", true));

        if (updateCount > 0) {
            NowSemester.setNowSemester(terms.getSemester());//更新nowSemester
            return Res.ok(terms);
        } else {
            return Res.failed("设置当前学期失败");
        }
    }

    @Override
    public Res deleteSemester(String semester) {
        int deleteCount = termsMapper.delete(new QueryWrapper<Terms>().eq("semester", semester));
        if (deleteCount > 0) {
            return Res.ok("删除学期成功");
        } else {
            return Res.failed("删除学期失败");
        }
    }

    @Override
    public Res addClasstable(Classtable classtable) {
        int insertCount = classtableMapper.insert(classtable);
        if (insertCount > 0) {
            return Res.ok(null);
        } else {
            return Res.failed("管理员添加排课失败");
        }
    }
}




